package vl.scanfile.cmmn.util;

import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;

public class CmmUtil {

	private static Gson gson = null; 
	public static final String patternRegex = "\\{\\d+\\}";
	
	public static Gson getGsonInstance(){
		
		if( gson == null){
			gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create();
			//.addDeserializationExclusionStrategy(new GsonSerializedExcludeCustom()).create();
		}
		return gson;
	}
	
	public static String getGUID()	{
		return UUID.randomUUID().toString();
	}
	
	public static boolean isNull(Object str) {

        if (str == null || str.toString().toLowerCase().equals("null")
                || str.toString().trim().equals("")) {
            return true;
        }
		return false;
    }
	
	public static Object jsonToObject(String json, Type objType){		
		
		return getGsonInstance().fromJson(json, objType);
	}
	
	public static Object jsonToObject(Reader json, Type objType){		
		
		return getGsonInstance().fromJson(json, objType);
	}
	
	public static String objToJson( Object obj){
		
		return getGsonInstance().toJson(obj);
	}
	
	public static String StringMatchBind(String str, Object[] obj){
		
		Pattern pattern = Pattern.compile(patternRegex);
		Matcher matcher = pattern.matcher(str);
		
		int i=0;
		try{
			while( matcher.find()){
				str = 	str.replace( matcher.group(), obj[i].toString());
				i++;
			}
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
		return str;
	}

	public static List<HashMap<String, String>> jsonToHashMapList(JsonReader json) {
		Gson gson = new Gson();
		return gson.fromJson(json, new TypeToken<List<HashMap<String, String>>>(){}.getType());
	}
	public static List<HashMap<String, String>> jsonToHashMapList(String input) {
		//pre-handle input string
		String jsonData = input.replaceAll("&quot;", "\"");
		jsonData = jsonData.replaceAll("false", "N");
		jsonData = jsonData.replaceAll("true", "Y");
		jsonData = jsonData.replaceAll("\"AR\":\"\"","\"AR\":\"0.0\"");
		jsonData = jsonData.replaceAll("\"SHARE_RT\":\"\"","\"SHARE_RT\":\"0\"");
		JsonReader reader = new JsonReader(new StringReader(jsonData));
		reader.setLenient(true);
		Gson gson = new Gson();
		return gson.fromJson(reader, new TypeToken<List<HashMap<String, String>>>(){}.getType());
	}

	public static String mathRound(String pi){
		
		double pib = 0;
		if(pi != null)
			pib = Double.parseDouble(pi);
		String str = "";
		str = str + (double)Math.round(pib*10)/10;
		return str;
	}
	
  public static String repeat(String s, int n) {
    if(s == null) {
        return null;
    }
    final StringBuilder sb = new StringBuilder();
    for(int i = 0; i < n; i++) {
        sb.append(s);
    }
    return sb.toString();
  }
	//Ä�á»�c sá»‘ hÃ ng chá»¥c
    public static String dochangchuc(double so, boolean daydu)
    {
    	String[] mangso = {"khÃ´ng","má»™t","hai","ba","bá»‘n","nÄƒm","sÃ¡u","báº£y","tÃ¡m","chÃ­n"};
    	String chuoi = "";
        int chuc = (int)Math.floor(so / 10);
        int donvi = (int)so % 10;
        if (chuc > 1)
        {
            chuoi = " " + mangso[chuc] + " mÆ°Æ¡i";
            if (donvi == 1)
            {
                chuoi += " má»‘t";
            }
        }
        else if (chuc == 1)
        {
            chuoi = " mÆ°á»�i";
            if (donvi == 1)
            {
                chuoi += " má»™t";
            }
        }
        else if (daydu && donvi > 0)
        {
           // chuoi = "láº»";
        	chuoi = " láº»";
        }
        if (donvi == 5 && chuc >= 1)
        {
            chuoi += " lÄƒm";
        }
        else if (donvi > 1 || (donvi == 1 && chuc == 0))
        {
            chuoi += " " + mangso[donvi];
        }
        return chuoi;
    }
    //Ä�á»�c block 3 sá»‘
    public static String docblock(double so, boolean daydu)
    {
    	String[] mangso = {"khÃ´ng","má»™t","hai","ba","bá»‘n","nÄƒm","sÃ¡u","báº£y","tÃ¡m","chÃ­n"};
    	String chuoi = "";
        int tram = (int)Math.floor(so / 100);
        so = so % 100;
        if (daydu || tram > 0)
        {
            chuoi = " " + mangso[tram] + " trÄƒm";
            chuoi += dochangchuc(so, true);
        }
        else
        {
            chuoi = dochangchuc(so, false);
        }
        return chuoi;
    }
    //Ä�á»�c sá»‘ hÃ ng triá»‡u
    public static String dochangtrieu(double so, boolean daydu)
    {
    	
        String chuoi = "";
        int trieu = (int)Math.floor(so / 1000000);
        so = so % 1000000;
        if (trieu > 0)
        {
            chuoi = docblock(trieu, daydu) + " trieu, ";
            daydu = true;
        }
        double nghin = Math.floor(so / 1000);
        so = so % 1000;
        if (nghin > 0)
        {
            chuoi += docblock(nghin, daydu) + " nghin, ";
            daydu = true;
        }
        if (so > 0)
        {
            chuoi += docblock(so, daydu);
        }
        return chuoi;
    }

    //Ä�á»�c sá»‘
    public static String docso(double so)
    {
    	String[] mangso = {"khÃ´ng","má»™t","hai","ba","bá»‘n","nÄƒm","sÃ¡u","báº£y","tÃ¡m","chÃ­n"};
        if (so == 0) return mangso[0];
        String chuoi = "", hauto = "";
        do
        {
            double ty = so % 1000000000;
            so = Math.floor(so / 1000000000);
            if (so > 0)
            {
                chuoi = dochangtrieu(ty, true) + hauto + chuoi;
            }
            else
            {
                chuoi = dochangtrieu(ty, false) + hauto + chuoi;
            }
            hauto = " tá»·, ";
        } while (so > 0);
        try
        {
            if (chuoi.trim().substring(chuoi.trim().length() - 1, 1) == ",")
            { chuoi = chuoi.trim().substring(0, chuoi.trim().length() - 1); }
        }
        catch(Exception e) { }
        return chuoi.trim() + " ";
    }
    
	public static String docPhanThapPhan(String so) {
		String result = "";
		String[] mangso = {"khÃ´ng","má»™t","hai","ba","bá»‘n","nÄƒm","sÃ¡u","báº£y","tÃ¡m","chÃ­n"};
		for(int i = 0; i < so.length(); i++) {
			result += " " + mangso[so.charAt(i) - '0'];
		}
		return result;
	}
	
    public static String docSoCoPhanThapPhan(String so) {
		int index1 = so.indexOf(","); 
		int index2 = so.indexOf(".");
		
		if(index1 != -1) {
			String string1 = so.substring(0, index1);
			String string2 = so.substring(index1 + 1, so.length());
			
			return docso(Double.parseDouble(string1)) + " phẩy" + docPhanThapPhan(string2);
		}
		else if(index2 != -1) {
			String string1 = so.substring(0, index2);
			String string2 = so.substring(index2 + 1, so.length());
			
			return docso(Double.parseDouble(string1)) + " phẩy" + docPhanThapPhan(string2);
		}
		else {
			return docso(Double.parseDouble(so));
		}
	}
	
	private static List quickSort1(List list, int lowerIndex, int higherIndex) {
		int i = lowerIndex;
		int j = higherIndex;
		Object pivot = list.get(lowerIndex+(higherIndex-lowerIndex)/2);
		while (i <= j) {
			while (((Comparable) list.get(i)).compareTo(pivot) == -1) {
				i++;
			}
			while (((Comparable) list.get(i)).compareTo(pivot)  == 1) {
				j--;
			}
			if (i <= j) {
				Object temp = list.get(i);
				list.set(i, list.get(j));
				list.set(j, temp);
				i++;
				j--;
			}
		}
		if (lowerIndex < j)
			list = quickSort1(list, lowerIndex, j);
		if (i < higherIndex)
			list = quickSort1(list, i, higherIndex);
		
		return list;
	}
	
	public static int findNearest(Object[] list, Object val) {
		int index = Arrays.binarySearch(list, val);
		if (index < 0)
		{
		    index = ~index - 1;
		}
		
		return index;
/*		if(list.size() <= 0) {
			return -1;
		}
	    int lowest = 0, mid;
	    int highest = list.size() - 1;
	    while (true) {    
	        if (lowest == highest) {
	            return(lowest);
	        }
	        mid = (int) Math.ceil(((highest - lowest) / 2.0) + lowest);
	        if (((Comparable) list.get(mid)).compareTo(val) == 0) {
	            return(mid);
	        }
	        else if (((Comparable) list.get(mid)).compareTo(val) == -1) {
	            lowest = mid;
	        } else {
	            highest = Math.max(lowest, mid - 1);
	        }
	    }*/
	}
	public static String getTextNumber(String inputTxt, int Num){
		
		String outPut = inputTxt;
		if(inputTxt.length() > 0 
				&& inputTxt.length() < Num){
			int tem = Num - inputTxt.length();
			
			for(int i=0; i < tem; i++){
				outPut = "0" + outPut;
			}
		}
		else 
			outPut = inputTxt;
		
		return outPut;
	}
}
